import loaddata from "./loaddata.js";
import render   from "./render.js";
import siteHeader from "./siteHeader.js";
import backTop    from "./backTop.js";
function CartsList(){}

$.extend(CartsList.prototype ,{
    init : function(){
        console.log("i am cartsList");
        //购物车
        this.myCart    = $(".myCart");
        //购物车主体
        this.cart_body = $(".cart-body");
        //结算购物车
        this.c_cf_amount= $(".c-cf-amount");
        //左上角图标
        this.cart_num    = $(".cart-num");
        this.sp_num      = $(".sp-num");
        
        this.sign   = $(".signin-btn");
        this.dLabel = $("#dLabel");

        //加载数据
        var res = loaddata.shop();
        res.then($.proxy(function(res){
            this.res = JSON.parse(res);
            //shop_total 包含html str
            
            var htmls = render.renderCart(this.res);
            //列表渲染
            this.myCart.html(htmls.html);
            this.c_cf_amount.html(htmls.str);       

            this.sumCartNum();
            this.cart_body.on("click",".btn-reduce,.btn-add",$.proxy(this.changeShopsNum,this));
            this.cart_body.on("click",".btn-link",$.proxy(this.delShopsNum,this));
            this.validateTocken();
        },this))
        siteHeader.init();
        backTop.init();
    },
    delShopsNum : function(evt){
        var e = evt || window.event;
        var target = e.target || e.srcElement;
        var id = $(target).attr("data-id");
        // console.log(id);
        var la = JSON.parse(localStorage.getItem("carts"));
        la.forEach($.proxy(function(item,index){
            if($(target).hasClass("btn-xs") && item.id === id){
                var flag = confirm("确定要删除吗?");
                console.log(flag);  
                if(flag){
                    la.splice(index,1);
                    localStorage.setItem("carts",JSON.stringify(la));
                    var htmls = render.renderCart(this.res);
        
                    this.myCart.html(htmls.html);
                    this.c_cf_amount.html(htmls.str);
                }              
               
            }
        },this)) 
    },
    changeShopsNum : function(evt){
        var e = evt || window.event;
        var target = e.target || e.srcElement;
        var id = $(target).parent().parent().attr("shop-id");

        var la = JSON.parse(localStorage.getItem("carts"));

        var $val = $($(target).parent().parent().children()[1])

        la.forEach($.proxy(function(item,index){   
            if($(target).hasClass("btn-reduce") && item.id === id){
                item.count --;
                if(item.count === 0) la.splice(index,1);    
                $val.val(item.count);
            }
            if( $(target).hasClass("btn-add") && item.id === id){
                item.count ++
                $val.val(item.count);
            }
        },this))
        localStorage.setItem("carts",JSON.stringify(la));

        var htmls = render.renderCart(this.res);
        
        this.myCart.html(htmls.html);
        this.c_cf_amount.html(htmls.str);

        this.sumCartNum();
    },
    sumCartNum : function(){
        var locals = localStorage.getItem("carts");
        var sum = 0;
        locals = locals === null ? "[]" : locals;
        locals = JSON.parse(locals);

        locals.forEach($.proxy(function(item){
            sum += item.count;        
        },this));
        this.cart_num.html(sum);
        sum !== 0 && this.sp_num.css("display","none") && $(".checkout-btn").removeClass("disabled"); 
        sum || this.sp_num.css("display","block") && $(".checkout-btn").addClass("disabled");          
    },
    validateTocken :function (){
        let url = "http://localhost/js/test/konka/php/login.php";
        $.ajax(url)
        .then( res => {
              res = JSON.parse(res)
              if(res.state){
                    return false;
              }        
              this.dLabel.html(res.username+",欢迎你");
              $(this.sign[0]).css("display","none");
              $(this.sign[1]).css("display","none");
              this.dLabel.on("click",function(){
                  $(".dropdown-menu").toggle(500);
              })
             
        })
    }
})

export default new CartsList();